package cmsapi

import (
	// "net/http"
	"encoding/json"
	"strconv"
	"time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
)

type (
	productiongroup struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		ProductID 					string `json:"productid" bson:"productid"`
		StyleNumber 				string `json:"stylenumber" bson:"stylenumber"`
		Cost 						string `json:"cost" bson:"cost"`
		Color						string `json:"color" bson:"color"`
		ClothingSupplier 			string `json:"clothingsupplier" bson:"clothingsupplier"`
		OrderDate 				 time.Time `json:"orderdate" bson:"orderdate"`
		OrderSKU 					string `json:"ordersku" bson:"ordersku"`
		InboundDate 			 time.Time `json:"inbounddate" bson:"inbounddate"`
		ErpInfoImport 				string `json:"erpinfoimport" bson:"erpinfoimport"`
		WhetherImportErpInfo		string `json:"whetherimporterpinfo" bson:"whetherimporterpinfo"`
		DueDatePicSample 			string `json:"duedatepicsample" bson:"duedatepicsample"`
		UploadTestReport 			[]string `json:"uploadtestreport" bson:"uploadtestreport"`
		UploadPdfTestReport 		[]string `json:"uploadpdftestreport" bson:"uploadpdftestreport"`
		UploadWaterReport 			[]string `json:"uploadwaterreport" bson:"uploadwaterreport"`
		CertificatePrintTemplate 	string `json:"certificateprinttemplate" bson:"certificateprinttemplate"`
		CertificatePrint			string `json:"certificateprint" bson:"certificateprint"`
		WatermarkPrintTemplate 		string `json:"watermarkprinttemplate" bson:"watermarkprinttemplate"`
		WatermarkPrint 				string `json:"watermarkprint" bson:"watermarkprint"`
		InboundDifferences			string `json:"inbounddifferences" bson:"inbounddifferences"`
		WatermarkNumber 			string `json:"watermarknumber" bson:"watermarknumber"`
		WatermarkSupplier 			string `json:"watermarksupplier" bson:"watermarksupplier"`
		WatermarkSingleMoney		string `json:"watermarksinglemoney" bson:"watermarksinglemoney"`
		CertificateNumber			string `json:"certificatenumber" bson:"certificatenumber"`
		CertificateSupplier			string `json:"certificatesupplier" bson:"certificatesupplier"`
		CertificateSingleMoney		string `json:"certificatesinglemoney" bson:"certificatesinglemoney"`
		SizemarkNumber 				string `json:"sizemarknumber" bson:"sizemarknumber"`
		SizemarkSupplier 			string `json:"sizemarksupplier" bson:"sizemarksupplier"`
		SizemarkSingleMoney			string `json:"sizemarksinglemoney" bson:"sizemarksinglemoney"`
		Status						string `json:"status" bson:"status"`
		Fabricprice					string `json:"fabricprice" bson:"fabricprice"`
		Fabricsupplier				string `json:"fabricsupplier" bson:"fabricsupplier"`
		Featherprice				string `json:"featherprice" bson:"featherprice"`
		Feathersupplier				string `json:"feathersupplier" bson:"feathersupplier"`
		Factorymodelnumber			string `json:"factorymodelnumber" bson:"factorymodelnumber"`
		SaleCost					string `json:"salecost" bson:"salecost"`
		MaterialGroup				string `json:"materialgroup" bson:"materialgroup"`
	}
	insertproductiongroup struct {
		ProductID 					string `json:"productid" bson:"productid"`
		StyleNumber 				string `json:"stylenumber" bson:"stylenumber"`
		Cost 						string `json:"cost" bson:"cost"`
		Color						string `json:"color" bson:"color"`
		ClothingSupplier 			string `json:"clothingsupplier" bson:"clothingsupplier"`
		OrderDate 				 time.Time `json:"orderdate" bson:"orderdate"`
		OrderSKU 					string `json:"ordersku" bson:"ordersku"`
		InboundDate 			 time.Time `json:"inbounddate" bson:"inbounddate"`
		ErpInfoImport 				string `json:"erpinfoimport" bson:"erpinfoimport"`
		WhetherImportErpInfo		string `json:"whetherimporterpinfo" bson:"whetherimporterpinfo"`
		DueDatePicSample 			string `json:"duedatepicsample" bson:"duedatepicsample"`
		UploadTestReport 			[]string `json:"uploadtestreport" bson:"uploadtestreport"`
		UploadPdfTestReport 		[]string `json:"uploadpdftestreport" bson:"uploadpdftestreport"`
		UploadWaterReport 			[]string `json:"uploadwaterreport" bson:"uploadwaterreport"`
		CertificatePrintTemplate 	string `json:"certificateprinttemplate" bson:"certificateprinttemplate"`
		CertificatePrint			string `json:"certificateprint" bson:"certificateprint"`
		WatermarkPrintTemplate 		string `json:"watermarkprinttemplate" bson:"watermarkprinttemplate"`
		WatermarkPrint 				string `json:"watermarkprint" bson:"watermarkprint"`
		InboundDifferences			string `json:"inbounddifferences" bson:"inbounddifferences"`
		WatermarkNumber 			string `json:"watermarknumber" bson:"watermarknumber"`
		WatermarkSupplier 			string `json:"watermarksupplier" bson:"watermarksupplier"`
		WatermarkSingleMoney		string `json:"watermarksinglemoney" bson:"watermarksinglemoney"`
		CertificateNumber			string `json:"certificatenumber" bson:"certificatenumber"`
		CertificateSupplier			string `json:"certificatesupplier" bson:"certificatesupplier"`
		CertificateSingleMoney		string `json:"certificatesinglemoney" bson:"certificatesinglemoney"`
		SizemarkNumber 				string `json:"sizemarknumber" bson:"sizemarknumber"`
		SizemarkSupplier 			string `json:"sizemarksupplier" bson:"sizemarksupplier"`
		SizemarkSingleMoney			string `json:"sizemarksinglemoney" bson:"sizemarksinglemoney"`
		Status						string `json:"status" bson:"status"`
		Fabricprice					string `json:"fabricprice" bson:"fabricprice"`
		Featherprice				string `json:"featherprice" bson:"featherprice"`
		Fabricsupplier				string `json:"fabricsupplier" bson:"fabricsupplier"`
		Feathersupplier				string `json:"feathersupplier" bson:"feathersupplier"`
		Factorymodelnumber			string `json:"factorymodelnumber" bson:"factorymodelnumber"`
		SaleCost					string `json:"salecost" bson:"salecost"`
		MaterialGroup				string `json:"materialgroup" bson:"materialgroup"`
	}
)

func GetProductionGroupListHandler(c echo.Context) error {
	currentpage, err:= strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery:= c.QueryParam("like")
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="production_group"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]productiongroup{}
	query.All(&ps)
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":string(str),
		"count":count,
	})
	return nil
}

func AddProductionGroupHandler(c echo.Context) error {
	p:= &insertproductiongroup{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="production_group"
	err:=model.DB().Insert(p)
	if err != nil {
		log.Error("插入出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
	})

	return nil	
}
func UpdateProductionGroupHandler(c echo.Context) error {
	id := c.QueryParam("id") 
	log.Info("UpdateProductionGroupHandler",id)
	
	p:= &insertproductiongroup{}
	if err := c.Bind(p); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="production_group"
	err:= model.DB().Update(bson.M{"_id":bson.ObjectIdHex(id)} , p)
	if err != nil{
		log.Error("更新production_group出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	model.DB().Document="products"
	selector := bson.M{"_id": bson.ObjectIdHex(p.ProductID)}
	data := bson.M{"$set": bson.M{"productionteam": p.Status,"color": p.Color,"cost": p.Cost}}
	err= model.DB().Update(selector, data)
	if err != nil{
		log.Error("更新products出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}
func DelProductionGroupHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelProductionGroupHandler",id)
	model.DB().Dbname="OxyCms"
	model.DB().Document="production_group"
	err:=model.DB().Remove(bson.M{"_id":bson.ObjectIdHex(id)})
	if err != nil{
		log.Error("删除出错",err)
		c.JSON(403, map[string]interface{}{
			"msg":  "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
	})

	return nil
}